home *** CD-ROM | disk | FTP | other *** search
/ Mac-Source 1994 July / Mac-Source_July_1994.iso / C and C++ / Text⁄Files / Voyeur 1.1.1 / Voyeur ƒ / MSG Shell ƒ / msg main.c < prev    next >
C/C++ Source or Header  |  1994-02-27  |  8KB  |  342 lines

  1. /**********************************************************************\
  2.  
  3. File:        msg main.c
  4.  
  5. Purpose:    This module handles the event loop and event dispatching.
  6.  
  7.  
  8. Voyeur -- a no-frills file viewer
  9. Copyright ©1993-4, Mark Pilgrim
  10.  
  11. This program is free software; you can redistribute it and/or modify
  12. it under the terms of the GNU General Public License as published by
  13. the Free Software Foundation; either version 2 of the License, or
  14. (at your option) any later version.
  15.  
  16. This program is distributed in the hope that it will be useful,
  17. but WITHOUT ANY WARRANTY; without even the implied warranty of
  18. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  19. GNU General Public License for more details.
  20.  
  21. You should have received a copy of the GNU General Public License
  22. along with this program in a file named "GNU General Public License".
  23. If not, write to the Free Software Foundation, 675 Mass Ave,
  24. Cambridge, MA 02139, USA.
  25.  
  26. \**********************************************************************/
  27.  
  28. #include "msg main.h"
  29. #include "msg integrity.h"
  30. #include "msg graphics.h"
  31. #include "msg about.h"
  32. #include "msg help.h"
  33. #include "msg menus.h"
  34. #include "msg prefs.h"
  35. #include "msg environment.h"
  36. #include "Mercutio API.h"
  37. #include "v.h"
  38. #include "v progress.h"
  39. #include "v window maintenance.h"
  40. #include "v error.h"
  41. #include "program globals.h"
  42. #include "AppleEvents.h"
  43. #include "EPPC.h"
  44.  
  45. void main(void)
  46. {
  47.     MaxApplZone();    
  48.     InitGraf(&thePort);
  49.     InitFonts();
  50.     FlushEvents(everyEvent, 0);
  51.     InitWindows();
  52.     InitMenus();
  53.     TEInit();
  54.     InitDialogs(0L);
  55.     InitCursor();
  56.     GetDateTime(&randSeed);
  57.     
  58.     CheckEnvironment();    
  59.     DoIntegrityCheck();    
  60.     InitMSGGraphics();
  61.     InitHelp();
  62.     PrefsError(PreferencesInit());
  63.     InitEnvironment();    
  64.     InitProgram();
  65.     EventLoop();    
  66.     ShutDownEnvironment();
  67.     ExitToShell();
  68. }
  69.  
  70. void EventLoop(void)
  71. {
  72.     EventRecord        theEvent;
  73.     int                i;
  74.     
  75.     while (!gDone)
  76.     {
  77.         SetCursor(&arrow);
  78.         HiliteMenu(0);
  79.         
  80.         if (FrontWindow()!=0L)
  81.         {
  82.             for (i=0; i<NUM_WINDOWS; i++)
  83.             {
  84.                 if (FrontWindow()==gTheWindow[i])
  85.                 {
  86.                     SetPort(gTheWindow[i]);
  87.                     i=NUM_WINDOWS;
  88.                 }
  89.             }
  90.             if (IsProgramWindow(FrontWindow()))
  91.                 gWhichFile=GetProgramWindowIndex(FrontWindow());
  92.         }
  93.             
  94.         WaitNextEvent(everyEvent, &theEvent, gIsInBackground ? 100 :
  95.             (((gTheWindow[kAboutMSG]!=0L) && (FrontWindow()==gTheWindow[kAboutMSG])) ?
  96.             0 : (gNeedOpenDialog ? 0 : 30)), 0L);
  97.         DispatchEvents(theEvent);
  98.         
  99.         if ((!gIsInBackground) && (gTheWindow[kAboutMSG]!=0L) &&
  100.             (FrontWindow()==gTheWindow[kAboutMSG]))
  101.             DoTheMSGThing();
  102.     }
  103. }
  104.  
  105. void DispatchEvents(EventRecord theEvent)
  106. {
  107.     int                i;
  108.     OSErr            isHuman;
  109.     Point            thisPoint;
  110.     int                index;
  111.     char            thisChar;
  112.     unsigned long    dummy;
  113.     WindowPtr        theWindow;
  114.     
  115.     index=-1;
  116.     if ((theWindow=FrontWindow())!=0L)
  117.     {
  118.         for (i=0; (i<NUM_WINDOWS) && (index==-1); i++)
  119.             if (theWindow==gTheWindow[i])
  120.                 index=i;
  121.     }
  122.     
  123.     switch (theEvent.what)
  124.     {
  125.         case nullEvent:
  126.             if ((gNeedOpenDialog) && (!gIsInBackground))
  127.             {
  128.                 gNeedCounter++;
  129.                 if (gNeedCounter==2)
  130.                 {
  131.                     HandleError(NewEditWindow());
  132.                     gNeedOpenDialog=FALSE;
  133.                 }
  134.             }
  135.             break;
  136.         case mouseDown:
  137.             HandleMouseDown(theEvent);
  138.             break;
  139.         case keyDown:
  140.         case autoKey:
  141.             thisChar=(char)(theEvent.message & charCodeMask);
  142.             if (theEvent.modifiers & cmdKey)
  143.             {
  144.                 AdjustMenus();
  145.                 dummy=PowerMenuKey(theEvent.message, theEvent.modifiers, gFileMenu);
  146.                 if (dummy==0L)
  147.                     dummy=PowerMenuKey(theEvent.message, theEvent.modifiers, gOptionsMenu);
  148.                 if (dummy==0L)
  149.                     dummy=MenuKey(thisChar);
  150.                 HandleMenu(dummy);
  151.             }
  152.             else
  153.                 switch (index)
  154.                 {
  155.                     case kAbout:
  156.                         CloseTheWindow(index);
  157.                         break;
  158.                     case kAboutMSG:
  159.                         UpdateTheWindow(index);
  160.                         Delay(30, &dummy);
  161.                         CloseTheWindow(index);
  162.                         break;
  163.                     case kHelp:
  164.                         HelpKeyEvent(thisChar);
  165.                         break;
  166.                     default:
  167.                         if (IsProgramWindow(theWindow))
  168.                             ProgramKeyEvent(thisChar);
  169.                         break;
  170.                 }
  171.             break;
  172.         case diskEvt:
  173.             if (HiWord(theEvent.message)!=noErr)
  174.             {
  175.                 DILoad();
  176.                 SetPt(&thisPoint, 120, 120);
  177.                 isHuman=DIBadMount(thisPoint, theEvent.message);
  178.                 DIUnload();
  179.             }
  180.             break;
  181.         case updateEvt:
  182.             theWindow=(WindowPtr)theEvent.message;
  183.             
  184.             BeginUpdate(theWindow);
  185.             
  186.             if (IsProgramWindow(theWindow))
  187.                 UpdateProgramWindow(GetProgramWindowIndex(theWindow), FALSE);
  188.             else
  189.             {
  190.                 for (i=0; i<NUM_WINDOWS; i++)
  191.                     if ((WindowPtr)theEvent.message == gTheWindow[i])
  192.                         UpdateTheWindow(i);
  193.                         
  194.                 if ((theWindow!=0L) && (theWindow==gProgressDlog))
  195.                     UpdateDialog(theWindow, theWindow->visRgn);
  196.             }
  197.             
  198.             EndUpdate(theWindow);
  199.             break;
  200.         case activateEvt:
  201.             if (((WindowPtr)theEvent.message==gTheWindow[kAboutMSG]) &&
  202.                 ((theEvent.modifiers&activeFlag)==0))
  203.                 UpdateTheWindow(kAboutMSG);
  204.             break;
  205.         case osEvt:
  206.             if (((theEvent.message>>24)&0x0FF)==suspendResumeMessage)
  207.             {
  208.                 gIsInBackground=((theEvent.message&resumeFlag)==0);
  209.                 if ((gIsInBackground) && ((gTheWindow[kAboutMSG]!=0L) &&
  210.                     (FrontWindow()==gTheWindow[kAboutMSG])))
  211.                     UpdateTheWindow(kAboutMSG);
  212.                 if ((!gIsInBackground) && (gPendingResultCode!=allsWell))
  213.                 {
  214.                     if (gHasNotificationManager)
  215.                         NMRemove(&gMyNotification);
  216.                     HandleError(gPendingResultCode);
  217.                     gPendingResultCode=allsWell;
  218.                 }
  219.             }
  220.             break;
  221.         case kHighLevelEvent:
  222.             if (gHasAppleEvents)
  223.                 AEProcessAppleEvent(&theEvent);
  224.             break;
  225.     }
  226. }
  227.  
  228. void HandleMouseDown(EventRecord theEvent)
  229. {
  230.     WindowPtr            theWindow;
  231.     int                    windowCode;
  232.     long                windSize;
  233.     GrafPtr                oldPort;
  234.     int                    i;
  235.     Rect                sizeRect;
  236.     Boolean                gotone;
  237.     int                    index;
  238.     unsigned long        dummy;
  239.     
  240.     windowCode=FindWindow(theEvent.where, &theWindow);
  241.     index=-1;
  242.     for (i=0; (i<NUM_WINDOWS) && (index==-1); i++)
  243.         if (theWindow==gTheWindow[i])
  244.             index=i;
  245.     
  246.     switch (windowCode)
  247.     {
  248.         case inMenuBar:
  249.             AdjustMenus();
  250.             HandleMenu(MenuSelect(theEvent.where));
  251.             break;
  252.         case inContent:
  253.             if (FrontWindow() != theWindow)
  254.             {
  255.                 if (FrontWindow()==gProgressDlog)
  256.                     SysBeep(7);
  257.                 else
  258.                 {
  259.                     if (FrontWindow()==gTheWindow[kAboutMSG])
  260.                         UpdateTheWindow(kAboutMSG);
  261.                     if (IsProgramWindow(theWindow))
  262.                         SelectProgramWindow(theWindow);
  263.                     else
  264.                         SelectWindow(theWindow);
  265.                 }
  266.             }
  267.             else
  268.                 switch (index)
  269.                 {
  270.                     case kAbout:
  271.                         CloseTheWindow(index);
  272.                         break;
  273.                     case kAboutMSG:
  274.                         UpdateTheWindow(index);
  275.                         Delay(30, &dummy);
  276.                         CloseTheWindow(index);
  277.                         break;
  278.                     case kHelp:
  279.                         HelpEvent();
  280.                         break;
  281.                     default:
  282.                         if (IsProgramWindow(theWindow))
  283.                             ProgramEvent();
  284.                         break;
  285.                 }
  286.             break;
  287.         case inSysWindow:
  288.             SystemClick(&theEvent, theWindow);
  289.             break;
  290.         case inDrag:
  291.             DragWindow(theWindow, theEvent.where, &gDragRect);
  292.             if (index>=0)
  293.                 gWindowBounds[index] = (*(((WindowPeek)gTheWindow[index])->contRgn))->rgnBBox;
  294.             break;
  295.         case inGoAway:
  296.             if (TrackGoAway(theWindow, theEvent.where))
  297.                 DoTheCloseThing(theWindow);
  298.             break;
  299.         case inGrow:
  300.             sizeRect = screenBits.bounds;
  301.             OffsetRect(&sizeRect, sizeRect.left, sizeRect.top);
  302.             
  303.             windSize = GrowWindow(theWindow, theEvent.where, &sizeRect);
  304.             if(windSize != 0)
  305.             {
  306.                 GetPort(&oldPort);
  307.                 SetPort(theWindow);
  308.                 EraseRect(&theWindow->portRect);
  309.                 SizeWindow(theWindow, LoWord(windSize), HiWord(windSize), TRUE);
  310.                 InvalRect(&theWindow->portRect);
  311.                 SetPort(oldPort);
  312.             }
  313.             
  314.             if (index>=0)
  315.                 gWindowBounds[index] = (*(((WindowPeek)gTheWindow[index])->contRgn))->rgnBBox;
  316.             
  317.             break;
  318.         case inZoomIn:
  319.         case inZoomOut:
  320.             if(TrackBox(theWindow, theEvent.where, windowCode))
  321.             {
  322.                 GetPort(&oldPort);
  323.                 SetPort(theWindow);
  324.                 ZoomWindow(theWindow, windowCode, FALSE);
  325.                 InvalRect(&theWindow->portRect);
  326.                 SetPort(oldPort);
  327.             }
  328.             
  329.             if (index>=0)
  330.                 gWindowBounds[index] = (*(((WindowPeek)gTheWindow[index])->contRgn))->rgnBBox;
  331.             
  332.             break;
  333.     }
  334. }
  335.  
  336. void ShutDownEnvironment(void)
  337. {
  338.     ShutDownProgram();
  339.     ShutDownMSGGraphics();
  340.     ShutDownHelp();
  341. }
  342.